package com.example.mobile2021606185zhn0628;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;

import com.example.mobile2021606185zhn0628.fragment.hdFragment;
import com.example.mobile2021606185zhn0628.fragment.htFragment;
import com.example.mobile2021606185zhn0628.fragment.scFragment;
import com.example.mobile2021606185zhn0628.fragment.xwttFragment;
import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity {


    private androidx.appcompat.widget.Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private FrameLayout frameLayout;
    private NavigationView navigation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
            @Override
            public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); }
        };
        toggle.syncState();
        drawerLayout.addDrawerListener(toggle);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.framelayout, xwttFragment.newInstance("",""),"")
                .commit();
        toolbar.setTitle("新闻头条");
        navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                item.setCheckable(true);
                switch (item.getItemId()){
                    case R.id.itemsy:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.framelayout,xwttFragment.newInstance("",""),"")
                                .commit();
                        toolbar.setTitle("新闻头条");
                        break;
                    case R.id.itemht:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.framelayout,htFragment.newInstance("",""),"")
                                .commit();
                        toolbar.setTitle("我的话题");
                        break;
                    case R.id.itemsc:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.framelayout,scFragment.newInstance("",""),"")
                                .commit();
                        toolbar.setTitle("收藏");
                        break;
                    case R.id.itemhd:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.framelayout, hdFragment.newInstance("",""),"")
                                .commit();
                        toolbar.setTitle("活动");
                        break;
                    case R.id.itemtc:
                        MainActivity.this.finish();
                        break;
                }
                drawerLayout.closeDrawers();
                return true;
            }
        });
    }

    @SuppressLint("WrongViewCast")
    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        frameLayout = (FrameLayout) findViewById(R.id.framelayout);
        navigation = (NavigationView) findViewById(R.id.navigation);
    }
}